package com.hanbang.baseproject.view.login.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hanbang.baseproject.R;
import com.hanbang.baseproject.code.base.view.activity.BaseMvpActivity;
import com.hanbang.baseproject.mode.javabean.base.UserData;
import com.hanbang.baseproject.pressenter.othre.LoginPresenter;
import com.hanbang.baseproject.utils.other.CountdownUtils;
import com.hanbang.baseproject.utils.other.EditHelper;
import com.hanbang.baseproject.utils.other.Validators;
import com.hanbang.baseproject.utils.ui.SnackbarUtil;
import com.hanbang.baseproject.utils.ui.ToastUtils;
import com.hanbang.baseproject.utils.ui.UiUtils;
import com.hanbang.baseproject.view.home.activity.HomeActivity;
import com.hanbang.baseproject.view.login.iview.IloginView;
import com.hanbang.baseproject.widget.button.FlatButton;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by yang on 2017/5/3 2:30
 */
public class LoginActivity extends BaseMvpActivity<IloginView, LoginPresenter> implements IloginView {
    public EditHelper editHelper = new EditHelper(this);
    boolean isSkipHome;

    @Override
    public int getContentView() {
        return R.layout.activity_login;
    }

    /**
     * 跳转到主页
     *
     * @param context
     */
    public static void startUI(Context context) {
        startUI(context, true);
    }

    /**
     * 返回到当前页
     *
     * @param context
     * @param isSkipHome
     */
    public static void startUI(Context context, boolean isSkipHome) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("isSkipHome", isSkipHome);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    @Override
    public void parseIntent(Intent intent) {
        isSkipHome = intent.getBooleanExtra("isSkipHome", true);
    }

    /*
     * 初始化view
     */
    public void initView() {
        toolbar.setTitle("登录微信");
        toolbar.setBack(this);


    }


    /**
     * 获取验证码
     */
    @OnClick(value = {R.id.login_send_verification, R.id.user_login_row})
    protected void onClickListener(View v) {
        UiUtils.exitInput(LoginActivity.this);
        switch (v.getId()) {

            //获取验证码
            case R.id.login_send_verification:
                if (!editHelper.check(R.id.login_name)) {
                    return;
                }
                presenter.getHttpData(LoginActivity.this, editHelper.getText(R.id.login_name));
                break;

            //登录
            case R.id.user_login_row:
                showDialog("正在读取端口数据....");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        aaa();
                    }
                }, 6000);


                break;

        }
    }

    public void aaa() {
        showDialog("正在跳过手机验证....");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                HomeActivity.startUI(LoginActivity.this);

            }
        }, 6000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissDialog();

    }

    @Override
    public LoginPresenter initPressenter() {
        return new LoginPresenter();
    }


    @Override
    public void clearData() {

    }

    @Override
    public void isVerification(boolean whetherVerification) {

        if (whetherVerification) {
            SnackbarUtil.showLong(this, "发送成功", SnackbarUtil.Info).show();
            CountdownUtils.recordTime((TextView) findViewById(R.id.login_send_verification));
        }
    }

    @Override
    public void login(UserData UserData) {

        if (UserData.setDbUserData(UserData)) {
            ToastUtils.getToastShort("登录成功");
            //HomeActivity.startUI(this);
        } else {
            ToastUtils.getToastShort("登录失败");

        }


    }

    @BindView(R.id.login_name)
    EditText loginName;
    @BindView(R.id.login_password)
    EditText loginPassword;
    @BindView(R.id.login_send_verification)
    TextView loginSendVerification;
    @BindView(R.id.user_login_row)
    FlatButton userLoginRow;


}
